<template>
  <a-modal
    :width="modalWidth"
    :visible="visible"
    :title="title"
    :confirm-loading="btnLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
  >
    <a-form layout="inline" :form="form" :label-col="{ style: { width: '120px' } }" @keyup.enter.native="handleOk">
      <a-form-item label="操作密码">
        <a-input
          allowClear
          placeholder="请输入"
          class="per100"
          v-decorator="['password', { rules: [{ required: true, message: '请输入操作密码' }] }]"
        ></a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import store from '@/store/'
import Api from '@/api/DeviceManage/DeviceList'
import { getAction, postAction } from '../../../api/manage'
export default {
  name: 'CheckReset',
  data() {
    return {
      title: '校验操作密码',
      modalWidth: 500,
      visible: false,
      form: this.$form.createForm(this),
      id: 0,
      pass: false,
      btnLoading: false
    }
  },
  created() {},
  methods: {
    // 展示弹窗
    showModal(record) {
      this.visible = true
      this.id = record.id
    },
    async checkPassword(data) {
      this.btnLoading = true
      await postAction(Api.checkPassword, data).then(res => {
        if (res.code === 0) {
          this.pass = true
        } else {
          this.pass = false
          this.btnLoading = false
          this.$message.error(res.message)
        }
      })
      if (!this.pass) return
      getAction(Api.reset, { id: this.id }).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message)
          this.visible = false
          this.$emit('ok')
        } else {
          this.$message.error(res.message)
        }
        this.btnLoading = false
      })
    },
    handleOk() {
      this.form.validateFields((err, values) => {
        if (!err) {
          const username = store.getters.userInfo.username
          this.checkPassword({ ...values, username })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.form.resetFields()
      this.$emit('close')
      this.visible = false
    }
  }
}
</script>

